<?php

namespace Module;

class ProfileModule  extends BaseModule {

    public function userAction() {
        $args = \Lib\Registry::get('args'); 
        $name = $args['User'];
        if (\Lib\Model\UserModel::checkUnique($name) == 1) {
            $user = \Lib\Model\UserModel::getByName($name);
            if (!empty($_SESSION['user']) && $_SESSION['user'] == $name) {
                if (!empty($_POST) &&
                        ($_POST['first_name'] != $user['first_name'] ||
                        $_POST['last_name'] != $user['last_name'] ||
                        $_POST['status'] != $user['status'] ||
                        $_POST['birth_date'] != $user['birth_date'] ||
                        $_POST['gender'] != $user['gender'])) {
                    if (\Lib\Model\UserModel::update($user['id'], $_POST)) {
                        $successText = 'Изменения сохранены';
                        $user['first_name'] = $_POST['first_name'];
                        $user['last_name'] = $_POST['last_name'];
                        $user['status'] = $_POST['status'];
                        $user['birth_date'] = $_POST['birth_date'];
                        $user['gender'] = $_POST['gender'];
                    } else {
                        $errorText = 'Ошибка при сохранении';
                    }
                }
                $title = \Lib\Registry::get('view')->__('Edit profile');
                return $this->showTemplate(__FUNCTION__, array('title'=> $title, 'user'=> $user), 'layout.php', 'Edit');
            } else {
                $title = $name . ' - '.\Lib\Registry::get('view')->__('User info');
                return $this->showTemplate(__FUNCTION__, array('title'=> $title, 'user'=> $user), 'layout.php', 'View');
            }
        }
    }
    
    public function profileMenuAction(){
        $user = \Lib\Model\UserModel::getUser();
        $admin = \Lib\Model\UserModel::isAdmin() ? TRUE : FALSE;
        return $this->showTemplate(__FUNCTION__, array('user'=> $user, 'admin'=> $admin), '');
    }
    
    public function aboutMeAction(){
        
        return $this->showTemplate(__FUNCTION__, array(
            'title'=> \Lib\Registry::get('view')->__('About me')
            ));
    }

}

?>
